/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package systemInterface;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import wiiscript.Main;

/**
 *
 * @author Evan
 */
public class Interface {
    private static HashMap<String, Integer> keyCode = new HashMap<String, Integer>();

    public native void keyDown(int keyCode);
    
    public native void keyUp(int keyCode);

    public static int getKeyCode(String keyName) {
        if(keyCode.size() == 0) { //Init keycode if this is the first call
            URL fileURL = null;
            if(Main.getOS() == Main.OS.Mac) {
                //US keyborad configuration
                fileURL = ClassLoader.getSystemResource("res/macKeyConfigUS.txt");
            } else if(Main.getOS() == Main.OS.Windows) {
                //US keyboard configuration
                fileURL = ClassLoader.getSystemResource("res/winKeyConfigUS.txt");
            }
            try {
                BufferedReader reader = new BufferedReader(new FileReader(fileURL.getPath()));
                String line;
                while((line = reader.readLine()) != null) {
                    if(line.contains(":")) {
                        String[] keyValue = line.trim().split(":");
                        if(keyValue.length != 2) continue;
                        keyCode.put(keyValue[0], Integer.parseInt(keyValue[1]));
                    }
                }
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return keyCode.get(keyName);
    }
}
